#ifndef SMFILE_H
#define SMFILE_H

#include <QFile>
#include <QObject>
#include <QString>
#include <QtGlobal>

class smFile : public QFile {
  Q_OBJECT
public:
  smFile(QString fileName = "");
  void setBigFileName(QString &);       /* 设置文件名字 */
  bool openBigFile();                   /* 打开文件 */
  void setReadSize(qint64 size = 1024); /* 设置读取块的大小 */
  qint64 getBlockCount();               /* 获取块数量 */
  void setSeek(qint64 pos = 0);
  bool openBigFile(QIODevice::OpenMode mode,
                   QFileDevice::FileHandleFlags handleFlags = DontCloseHandle);

  uchar *prevSeek(void);       /* 读取上一块数据 */
  uchar *nextSeek(void);       /* 每次偏移+ blockSize大小 */
  uchar *nextSeek(qint64 pos); /* seek到pos位置开始读取 */

  void closeBigFile(); /* 关闭文件 */

signals:
  void sErrorInfo(QString); /* 错误信号 */

private:
  qint64 currentPos;
  qint64 blockSize;     /* 单次读取块的大小 */
  int blockCount;       /* 块数量 */
  QString openFileName; /* 文件名 */
  uchar *fPtr;          /* 文件指针 */
};

#endif // SMFILE_H

#if 0 /* example */

#include "smfile.h"
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget {
  Q_OBJECT

public:
  Widget(QWidget *parent = nullptr);
  ~Widget();

private slots:
  void on_pushButton_clicked();

private:
  Ui::Widget *ui;
  smFile *bigFile;
};

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
  ui->setupUi(this);
  bigFile = new smFile();
  bigFile->setReadSize(2 * 1024 * 1024);
}

Widget::~Widget() {
  delete bigFile;
  delete ui;
}

void Widget::on_pushButton_clicked() {
  QString sPath = QFileDialog::getOpenFileName(
      this, QString("保存日志文件"),
      QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
      tr("Text(*.txt *.ini *.log)"));

  if (sPath.isEmpty()) {
    QMessageBox::information(this, "警告", "未选择文件!\n");
    return;
  }
  bigFile->setBigFileName(sPath);
  if (false == bigFile->openBigFile(QIODevice::ReadOnly | QIODevice::Text)) {
    return;
  }

  ui->textEdit->setText("blockSize:" +
                        QString::number(bigFile->getBlockCount()));
  QString tmp;
  for (int i = 0; i < bigFile->getBlockCount(); i++) {
    tmp = QString::fromLocal8Bit((char *)bigFile->nextSeek(), 2 * 1024 * 1024);
    qDebug() << "cnt is" << i << "\tdata" << tmp;
  }
}

#endif
